package com.huaiwei.usmManager.modules.usm.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaiwei.usmManager.common.utils.Constant;
import com.huaiwei.usmManager.common.utils.NumberUtils;
import com.huaiwei.usmManager.common.utils.PageUtils;
import com.huaiwei.usmManager.common.utils.Query;
import com.huaiwei.usmManager.modules.usm.dao.GoodsDao;
import com.huaiwei.usmManager.modules.usm.dao.TypeDao;
import com.huaiwei.usmManager.modules.usm.entity.*;
import com.huaiwei.usmManager.modules.usm.service.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class GoodsServiceImpl extends ServiceImpl<GoodsDao, GoodsEntity> implements GoodsService {
    @Autowired
    private RGoodsTypeService rGoodsTypeService;
    @Autowired
    private TypeDao typeDao;/*查询typelist*/
    @Autowired
    private TypeService typeService;
    @Autowired
    private ResourceService resourceService;
    @Autowired
    private GoodsSpecService goodsSpecService;
    @Autowired
    private CabinetService cabinetService;
    @Autowired
    private ShopService shopService;
    @Autowired
    private SalesService salesService;
    @Autowired
    private GoodsDao goodsDao;/*查询top10*/

    /**
     *分页
     * @param params
     * @return
     */
    @Override
    public PageUtils getPage(Map<String, Object> params) {
        String name=(String) params.get("name");
        Integer cabinetId= NumberUtils.obj2Integer(params.get("cabinetId"));
        IPage<GoodsEntity> page=this.page(new Query<GoodsEntity>().getPage(params),new QueryWrapper<GoodsEntity>()
        .like(StringUtils.isNotEmpty(name),"name",name)
        .eq(cabinetId!=null,"cabinet_id",cabinetId)
         .orderByDesc("goods_id"));
        return new PageUtils(page);
    }

    /**
     *查询商品具体信息包括类型
     * @param goodsId
     * @return
     */
    @Override
    public GoodsEntity getInfoByGoodsId(Integer goodsId) {
        GoodsEntity goodsEntity=this.getById(goodsId);/*先拿到商品的信息*/
        goodsEntity.setTypeList(getTypeListByGoodsId(goodsId));/*存入类型的list*/
        goodsEntity.setImageList(getResourceListByGoodsId(goodsId));/*存入图片的list*/
        return goodsEntity;
    }
    //查询商品的Typelist信息
    @Override
    public List<TypeEntity> getTypeListByGoodsId(Integer goodsId){
        return typeDao.getTypeListByGoodsId(goodsId);
    }
    //查询商品的Resourcelist信息
    @Override
    public List<ResourceEntity> getResourceListByGoodsId(Integer goodsId){
        return resourceService.list(new QueryWrapper<ResourceEntity>()
        .eq("resource_id",goodsId)
        .eq("type",Constant.GOODS)
        .eq("status",Constant.ON));
    }
    /**
     *新增/更新信息
     * @param goodsEntity
     */
    @Override
    public void insertOrUpdate(GoodsEntity goodsEntity) {
        if(goodsEntity.getGoodsId()!=null){
            deleteRelationInfo(goodsEntity.getGoodsId());/*删除关联信息完之后往下运行，更新即是新增*/
        }
        if(this.saveOrUpdate(goodsEntity)){/*如果商品添加成功了，那么需要添加相应的关系表*/
//         对rgoodstype表进行增加数据
            List<RGoodsTypeEntity> rgoodsTypeList=new ArrayList<>();
          if(goodsEntity.getTypeIds()!=null&&goodsEntity.getTypeIds().length>0){/*如果前端给的TypeIds不为空*/
              for(Integer id:goodsEntity.getTypeIds()){
                  RGoodsTypeEntity rGoodsTypeEntity=new RGoodsTypeEntity();/*新建实体类*/
                  rGoodsTypeEntity.setGoodsId(goodsEntity.getGoodsId());
                  rGoodsTypeEntity.setTypeId(id);
                  rGoodsTypeEntity.setStatus(Constant.ON);

                  rgoodsTypeList.add(rGoodsTypeEntity);/*实体类加入列表*/
              }
          }
          rGoodsTypeService.saveBatch(rgoodsTypeList);/*保存在rgoodstype数据库里*/
//          对resource表进行增加数据
            List<ResourceEntity> imageList=new ArrayList<>();
            if(goodsEntity.getImageUrls()!=null&&goodsEntity.getImageUrls().length>0){
                for(String imageUrl:goodsEntity.getImageUrls()){
                    ResourceEntity resourceEntity=new ResourceEntity();/*新建实体类*/
                    resourceEntity.setResourceId(goodsEntity.getGoodsId());
                    resourceEntity.setType(Constant.GOODS);
                    resourceEntity.setImageUrl(imageUrl);
                    resourceEntity.setStatus(Constant.ON);

                    imageList.add(resourceEntity);/*实体类加入列表*/
                }
            }
            resourceService.saveBatch(imageList);/*保存在resource数据库里*/
        }
    }

    /**
     * 删除信息
     * @param goodsId
     */
    @Override
    public void deleteInfo(Integer goodsId) {
       if(this.removeById(goodsId)){/*如果删除goods成功,那么继续删了相关的关系表*/
          deleteRelationInfo(goodsId);
       }
    }
    //删除关联的信息
    public void deleteRelationInfo(Integer goodsId){
        /*在type和goods关系表种删除goods_id的相关信息*/
        rGoodsTypeService.remove(new QueryWrapper<RGoodsTypeEntity>()
        .eq("goods_id",goodsId));
        /*在resource中删除商品的相关信息*/
        resourceService.remove(new QueryWrapper<ResourceEntity>()
        .eq("resource_id",goodsId)
        .eq("type",Constant.GOODS));
    }

    /**
     * 根据prospecId查询相应的规格
     * @param prospecId
     * @return
     */
    @Override
    public String getProSpecNameByProspecId(Integer prospecId){
        GoodsSpecEntity goodsSpecEntity=goodsSpecService.getById(prospecId);
        return goodsSpecEntity.getGoodsPack();
    }

    /**
     * 根据cabinetId查出具体的商品的位置
     * @param cabinetId
     * @return
     */
    @Override
    public String getMessageByCabinetId(Integer cabinetId){
      CabinetEntity cabinetEntity=cabinetService.getById(cabinetId);
      ShopEntity shopEntity=shopService.getById(cabinetEntity.getShopId());
      return shopEntity.getAddress()+"-"+shopEntity.getShopName()+"-"+cabinetEntity.getNumber()+"-"+shopEntity.getShopId();
    }

    /**
     * 级联查询,查出所有的shop
     * @return
     */
    @Override
    public List<ShopEntity> getAllShop() {
        return shopService.list(new QueryWrapper<ShopEntity>()
        .eq("status",Constant.ON));
    }

    /**
     *级联查询,根据shopid查出所属的cabinet
     * @param shopId
     * @return
     */
    @Override
    public List<CabinetEntity> getAllCabinetByShopId(Integer shopId) {
        return cabinetService.list(new QueryWrapper<CabinetEntity>()
        .eq("shop_id",shopId)
        .eq("status",Constant.ON));
    }

    /**
     * 根据goodsId查出他的销售记录(分页)
     * @param goodsId
     * @return
     */
    @Override
    public PageUtils getSalesByGoodsId(Integer goodsId,Map<String,Object> params){
        IPage<SalesEntity> page=salesService.page(new Query<SalesEntity>().getPage(params),
                new QueryWrapper<SalesEntity>().eq("goods_id",goodsId));
        return new PageUtils(page);
    }

    /**
     * 下拉框-查询所有可用的规格
     * @return
     */
    @Override
    public List<GoodsSpecEntity> getAllSpec(){
        return goodsSpecService.list(new QueryWrapper<GoodsSpecEntity>()
        .eq("status",Constant.ON));
    }

    /**
     * 下拉框—查询所有可用的type
     * @return
     */
    @Override
    public List<TypeEntity> getAllType(){
        return typeService.list(new QueryWrapper<TypeEntity>()
        .eq("status",Constant.ON));
    }

    /**
     * 新增一条规格记录
     * @param goodsSpecEntity
     */
    @Override
    public void insertProspec(GoodsSpecEntity goodsSpecEntity){
        goodsSpecEntity.setStatus(Constant.ON);/*默认为启用*/
        goodsSpecService.save(goodsSpecEntity);
    }

    /**
     * 首页报表-销售商品数量前十榜
     * @return
     */
    @Override
    public List<Top10Entity> getTopSales(){
       return goodsDao.getTopSales();
    }

    /**
     * 首页报表-商铺销售数量前五榜
     * @return
     */
    @Override
    public List<ShopTop5Entity> getTopShop(){
        return goodsDao.getTopShop();
    }


}
